<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="课题名称" prop="topicName">
        <el-input maxlength="50"
                  class="search-width"
                  v-model="queryParams.topicName"
                  placeholder="请输入课题名称"
                  clearable
                  @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="截止时间" prop="endTime">
        <el-date-picker class="search-width" clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择截止时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['biz:bizTopic:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['biz:bizTopic:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:bizTopic:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['biz:bizTopic:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bizTopicList"
              @selection-change="handleSelectionChange"
              :row-class-name="tableRowClassName"
              @row-dblclick="showDetail"
              border
              row-key="id">
      <el-table-column type="selection" width="55" align="left"/>
      <el-table-column label="序号" align="left" prop="indexNum" width="60"/>
      <el-table-column label="课题名称" align="left" prop="topicName" show-overflow-tooltip/>
      <el-table-column label="状态" align="left" prop="statusName" width="150"/>
      <el-table-column label="截止完成时间" align="left" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="left" prop="managerName" width="100" show-overflow-tooltip/>
      <el-table-column label="当前跟进部门" align="left" prop="deptName" width="180" show-overflow-tooltip/>
      <el-table-column label="创建人" align="left" prop="userName" width="100" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="left" prop="createTime" width="180"/>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-tooltip content="流转" placement="top" v-if="scope.row.status==1">
            <el-button link type="primary" icon="RefreshLeft" @click="handleSet(scope.row,0)"
                       v-hasPermi="['biz:bizTopic:replace']">
            </el-button>
          </el-tooltip>
          <el-tooltip content="完成" placement="top" v-if="scope.row.status==1">
            <el-button link type="primary" icon="Finished" @click="handleSet(scope.row,1)"
                       v-hasPermi="['biz:bizTopic:done']">
            </el-button>
          </el-tooltip>
          <el-tooltip content="开始" placement="top" v-if="scope.row.status==0">
            <el-button link type="primary" icon="Pointer" @click="handleStart(scope.row)"
                       v-hasPermi="['biz:bizTopic:start']">
            </el-button>
          </el-tooltip>
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['biz:bizTopic:edit']">
            </el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['biz:bizTopic:remove']">
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <topicAdd ref="topicAddRef" :projectId="topicProjectId" :topicId="topicAddId" @ok="updateTopicOk"/>
    <topicDetail ref="topicDetailRef" :topicId="topicDetailId"/>
    <topicSet ref="topicSetRef" :topicId="topicSetId" :setType="setTopicType" @ok="updateTopicOk"/>
  </div>
</template>


<script setup name="BizTopic">
import {listBizTopic, delBizTopic, startTopic} from "@/api/biz/bizTopic";
import selectUser from "@/views/system/user/selectUser";
import topicAdd from "@/views/biz/bizTopic/topicAdd.vue";
import topicDetail from "@/views/biz/bizTopic/topicDetail.vue";
import topicSet from "@/views/biz/bizTopic/topicSet.vue";

const route = useRoute();

const {proxy} = getCurrentInstance();

const bizTopicList = ref([]);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const topicProjectId = ref(0);
const topicAddId = ref(0);
const topicDetailId = ref(0);
const topicSetId = ref(0);
const setTopicType = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    topicName: null,
    projectId: null,
    parentId: 0,
    endTime: null,
    managerId: null,
    status: null,
  },
  rules: {
    projectId: [
      {required: true, message: "所属项目不能为空", trigger: "submit"}
    ],
    managerId: [
      {required: true, message: "负责人不能为空", trigger: "submit"}
    ],
    startTime: [
      {required: true, message: "预计开始时间不能为空", trigger: "submit"}
    ],
    endTime: [
      {required: true, message: "截止时间不能为空", trigger: "submit"}
    ],
    topicName: [
      {required: true, message: "课题名称不能为空", trigger: "submit"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);
const topicNameT = computed(() => route.query.topicName);
watch(topicNameT, (newValue, oldValue) => {
  queryParams.value.topicName = newValue;
  getList();
});

/** 查询子课题列表 */
function getList() {
  loading.value = true;
  listBizTopic(queryParams.value).then(response => {
    bizTopicList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  topicProjectId.value = null;
  topicAddId.value = null;
  nextTick(() => {
    proxy.$refs["topicAddRef"].show();
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  topicProjectId.value = null;
  topicAddId.value = row.id;
  nextTick(() => {
    proxy.$refs["topicAddRef"].show();
  });
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除子课题数据项？').then(function () {
    return delBizTopic(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

function updateTopicOk() {
  getList();
}

function showDetail(row) {
  topicDetailId.value = row.id;
  proxy.$refs["topicDetailRef"].show();
}

function handleSet(row, type) {
  setTopicType.value = type;
  topicSetId.value = row.id;
  proxy.$refs["topicSetRef"].show();
}

function handleStart(row) {
  proxy.$modal.confirm('是否确认开始[' + row.topicName + ']？').then(function () {
    return startTopic(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("开始成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('biz/bizTopic/export', {
    ...queryParams.value
  }, `bizTopic_${new Date().getTime()}.xlsx`)
}

const tableRowClassName = ({row, rowIndex}) => {
  if (row.due && row.status != 2) {
    return 'error-row'
  }
  if (row.warn && row.status != 2) {
    return 'warning-row'
  }
  return ''
}

getList();
</script>
<style>
.el-table .error-row {
  --el-table-tr-bg-color: var(--el-color-error-light-9);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
</style>
